package com.lianniu.web.tea.user.addr.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianniu.web.base.BaseResult;
import com.lianniu.web.tea.user.addr.model.TeaUserAddr;
import com.lianniu.web.tea.user.addr.service.ITeaUserAddrService;

@Controller
@RequestMapping("/rest/tea/user/addr")
public class TeaUserAddrController {
	
	private final static Logger logger = Logger.getLogger(TeaUserAddrController.class);

	@Autowired
	private ITeaUserAddrService teaUserAddrService;
	
	@RequestMapping(value="/getUserAddrList",method=RequestMethod.GET)
	public @ResponseBody BaseResult getUserAddrList(HttpServletRequest req){
		logger.info("TeaUserAddrController  getUserAddrList begin.");
		try {
			logger.info("getUserAddrList begin.");
			String userId = req.getParameter("userId");
			BaseResult baseResult = teaUserAddrService.getUserAddrList(userId);
			logger.info("getUserAddrList end.");
			logger.info("TeaUserAddrController getUserAddrList end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaUserAddrController getUserAddrList error:" + e.getMessage());
			logger.error("TeaUserAddrController getUserAddrList end.");
			return BaseResult.exception(e.getMessage());
		}
	}
	
	@RequestMapping(value="/setDefaultAddr",method=RequestMethod.GET)
	public @ResponseBody BaseResult setDefaultAddr(HttpServletRequest req){
		logger.info("TeaUserAddrController  setDefaultAddr begin.");
		try {
			logger.info("setDefaultAddr begin.");
			String userId = req.getParameter("userId");
			String addrId = req.getParameter("addrId");
			BaseResult baseResult = teaUserAddrService.setDefaultAddr(userId,addrId);
			logger.info("setDefaultAddr end.");
			logger.info("TeaUserAddrController setDefaultAddr end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaUserAddrController setDefaultAddr error:" + e.getMessage());
			logger.error("TeaUserAddrController setDefaultAddr end.");
			return BaseResult.exception(e.getMessage());
		}
	}
	
	@RequestMapping(value="/deleteAddr",method=RequestMethod.GET)
	public @ResponseBody BaseResult deleteAddr(HttpServletRequest req){
		logger.info("TeaUserAddrController  deleteAddr begin.");
		try {
			logger.info("deleteAddr begin.");
			String addrId = req.getParameter("addrId");
			BaseResult baseResult = teaUserAddrService.deleteAddr(addrId);
			logger.info("deleteAddr end.");
			logger.info("TeaUserAddrController deleteAddr end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaUserAddrController deleteAddr error:" + e.getMessage());
			logger.error("TeaUserAddrController deleteAddr end.");
			return BaseResult.exception(e.getMessage());
		}
	}
	
	@RequestMapping(value="/getAddrInfo",method=RequestMethod.GET)
	public @ResponseBody BaseResult getAddrInfo(HttpServletRequest req){
		logger.info("TeaUserAddrController  getAddrInfo begin.");
		try {
			logger.info("getAddrInfo begin.");
			String addrId = req.getParameter("addrId");
			BaseResult baseResult = teaUserAddrService.getAddrInfo(addrId);
			logger.info("getAddrInfo end.");
			logger.info("TeaUserAddrController getAddrInfo end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaUserAddrController getAddrInfo error:" + e.getMessage());
			logger.error("TeaUserAddrController getAddrInfo end.");
			return BaseResult.exception(e.getMessage());
		}
	}
	
	@RequestMapping(value="/addUserAddr",method={RequestMethod.GET,RequestMethod.POST})
	public @ResponseBody BaseResult addUserAddr(HttpServletRequest req){
		logger.info("TeaUserAddrController  addUserAddr begin.");
		try {
			logger.info("addUserAddr begin.");
			String userId = req.getParameter("userId");
			String userName = req.getParameter("userName");
			String userPhone = req.getParameter("userPhone");
			String userArea = req.getParameter("userArea");
			String userAddr = req.getParameter("userAddr");
			String isDefault = req.getParameter("isDefault");
			String addrId = req.getParameter("addrId");
			TeaUserAddr teaUserAddr = new TeaUserAddr();
			teaUserAddr.setId(addrId);
			teaUserAddr.setUserId(userId);
			teaUserAddr.setUserName(userName);
			teaUserAddr.setUserPhone(userPhone);
			teaUserAddr.setUserArea(userArea);
			teaUserAddr.setUserAddr(userAddr);
			teaUserAddr.setIsDefault(isDefault);
			BaseResult baseResult = teaUserAddrService.insert(teaUserAddr);
			logger.info("addUserAddr end.");
			logger.info("TeaUserAddrController addUserAddr end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaUserAddrController addUserAddr error:" + e.getMessage());
			logger.error("TeaUserAddrController addUserAddr end.");
			return BaseResult.exception(e.getMessage());
		}
	}
}
